{/* LAYOUT模板 */}
	<layout name="layout"/>
{/* LAYOUT模板 */}
{/* import Js,Css */}
	<js href="__PUBLIC__/js/admin/listTable.js"/>
	<js href="__PUBLIC__/js/admin/js.util.js"/>
	<css href="__PUBLIC__/css/admin/common.css"/>
	<js href="__PUBLIC__/js/admin/js.selectzone.js"/>
	
	<css href="__PUBLIC__/css/jquery-ui.css" />
	<js href="__PUBLIC__/js/core/jquery-ui.1.10.4.js" />
	<js href="__PUBLIC__/js/core/plugins.js"/>
{/* import js,css */}
<!-- [position] -->
	<include file="Template:position"/>
<!-- [position] -->

<div class="main-div">
	<form method="post" action="__URL__/{$action}" name="theForm" onsubmit="return validate()">
		<table cellspacing="1" cellpadding="3" width="100%">
			<tr>
				<td class="label" align="right">活动名称</td>
				<td>
					<input type="text" name="snatch_name" maxlength="60" size="40" value="{$snatch.snatch_name}" />*
				</td>
			</tr>
			<tr>
				<td align="right">商品关键字</td>
				<td>
					<input type="text" name="keywords" size="30" />
					<input type="button" value="搜索"  onclick="searchGoods()">
				</td>
			</tr>
			<tr>
				<td class="label" align="right">
					<a href="javascript:showNotice('noticegoodsid');" title="{$lang.form_notice}">
						<img src="__PUBLIC__/image/admin/notice.gif" width="16" height="16" border="0" alt="{$lang.form_notice}">
					</a>
					活动商品
				</td>
				<td>
					<select name="goods_id" onchange="javascript:change_good_products();">
						{$snatch.option}
					</select>
					<select name="product_id"  <if condition="$snatch['product_id'] elt 0">style="display:none"</if>>
						{html_options options=$good_products_select selected=$snatch.product_id}
					</select>
					*
					<br/>
					<span class="notice-span" <if condition="$help_open">style="display:block"<else/>style="display:none"</if> id="noticegoodsid">
						需要先搜索商品，生成商品列表，然后再选择
					</span>
				</td>
			</tr>
			<tr>
				<td class="label" align="right">活动开始时间</td>
				<td>
					<input type="text"  name="start_time" maxlength="60" size="40" value="{$snatch.start_time}" readonly="readonly" id="start_time_id" />					
					*
				</td>
			</tr>
			<tr>
				<td class="label" align="right">活动结束时间</td>
				<td>
					<input type="text" name="end_time" maxlength="60" size="40" value="{$snatch.end_time}"  readonly="readonly" id ="end_time_id" />
					*
				</td>
			</tr>
			<tr>
				<td class="label" align="right">
					<a href="javascript:showNotice('noticeminPrice');" title="{$lang.form_notice}">
						<img src="__PUBLIC__/image/admin/notice.gif" width="16" height="16" border="0" alt="{$lang.form_notice}">
					</a>
					价格下限
				</td>
				<td>
					<input type="text" name="start_price" maxlength="60" size="20" value="{$snatch.start_price}" />
					*<br />
					<span class="notice-span" <if condition="$help_open">style="display:block"<else/>style="display:none"</if> id="noticeminPrice">
						用户出价范围的下限
					</span>
				</td>
			</tr>
			<tr>
				<td class="label" align="right">
					<a href="javascript:showNotice('noticemaxPrice');" title="{$lang.form_notice}">
						<img src="__PUBLIC__/image/admin/notice.gif" width="16" height="16" border="0" alt="{$lang.form_notice}">
					</a>
					价格上限
				</td>
				<td>
					<input type="text" name="end_price" maxlength="60" size="20" value="{$snatch.end_price}" />
					*<br />
					<span class="notice-span" <if condition="$help_open">style="display:block"<else/>style="display:none"</if> id="noticemaxPrice">
						用户出价范围的上限
					</span>
				</td>
			</tr>
			<tr>
				<td class="label" align="right">
					<a href="javascript:showNotice('noticePrice');" title="{$lang.form_notice}">
						<img src="__PUBLIC__/image/admin/notice.gif" width="16" height="16" border="0" alt="{$lang.form_notice}">
					</a>
					最多需支付的价格
				</td>
				<td>
					<input type="text" name="max_price" maxlength="60" size="20" value="{$snatch.max_price}" />
					*<br />
					<span class="notice-span" <if condition="$help_open">style="display:block"<else/>style="display:none"</if> id="noticePrice">
						获奖者出价高于这个价格，则以这个价格购买该商品。为0时按用户出价购买商品
					</span>
				</td>
			</tr>
			<tr>
				<td class="label" align="right">
					<a href="javascript:showNotice('noticeintegral');" title="{$lang.form_notice}">
						<img src="__PUBLIC__/image/admin/notice.gif" width="16" height="16" border="0" alt="{$lang.form_notice}">
					</a>
					消耗积分
				</td>
				<td>	
					<input type="text" name="cost_points" maxlength="60" size="20" value="{$snatch.cost_points}" />
					*<br />
					<span class="notice-span" <if condition="$help_open">style="display:block"<else/>style="display:none"</if> id="noticeintegral">
						每次出价所消耗的积分值
					</span>
				</td>
			</tr>
			<tr>
				<td class="label" align="right">活动描述</td>
				<td>
					<textarea  name="desc" cols="60" rows="4"  >{$snatch.act_desc}</textarea>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="确定" />
					<input type="reset" value="重置"/>
					<input type="hidden" name="act" value="{$form_action}" />
					<input type="hidden" name="id" value="{$snatch.act_id}" />
				</td>
			</tr>
		</table>
	</form>
</div>

<script>	
	$(function() {
		$( "#start_time_id" ).datepicker({dateFormat: 'yy-mm-dd'});
		$( "#end_time_id" ).datepicker({dateFormat: 'yy-mm-dd'});
	});	
</script>


<script language="JavaScript">
var display_yes = (Browser.isIE) ? 'block' : 'table-row-group';

document.forms['theForm'].elements['snatch_name'].focus();
onload = function(){
  // 开始检查订单
  //startCheckOrder();
}

/**
 * 检查表单输入的数据
 */
function validate(){
  validator = new Validator("theForm");
  validator.required("snatch_name",  no_name);
  validator.required("desc",         no_desc);
  validator.isNullOption("goods_id", no_goods_id);
  validator.isTime("start_time", invalid_starttime, true);
  validator.isTime("end_time", invalid_endtime, true);
  validator.gt("end_time", "start_time", invalid_gt);
  validator.gt("end_price", "start_price", invalid_price);
  validator.isNumber("start_price", invalid_min_price, true);
  validator.isNumber("max_price", invalid_max_price, true);
  validator.isInt("cost_points", invalid_integral, true);

  if (document.forms['theForm'].elements['act'] == "insert"){
      validator.required("password", no_password);
  }

  return validator.passed();
}

/**
 * 搜索商品
 *
 */
function searchGoods(){
	var filter 		= 	new Object;
	filter.keyword 	= 	document.forms['theForm'].elements['keywords'].value;

	$.ajax({
		url: '__URL__/search_goods',
		type: 'get',
		dataType: 'json',
		data: {'jsON':filter},
		beforeSend:function(){},
		success:function(msg){
			searchGoodsResponse(msg);
		}		
	});
}

function searchGoodsResponse(result){
  var frm = document.forms['theForm'];
  var sel = frm.elements['goods_id'];
  var sp = frm.elements['product_id'];

  if (result.error == 0){
    /* 清除 options */
    sel.length = 0;
    sp.length = 0;

    /* 创建 options */
    var goods = result.content.goods;
    if (goods){
      for (i = 0; i < goods.length; i++){
          var opt = document.createElement("OPTION");
          opt.value = goods[i].goods_id;
          opt.text  = goods[i].goods_name;
          sel.options.add(opt);
      }
    }
    else{
      var opt = document.createElement("OPTION");
      opt.value = 0;
      opt.text  = search_is_null;
      sel.options.add(opt);
    }

    /* 创建 product options */
    var products = result.content.products;
    if (products){
      sp.style.display = display_yes;

      for (i = 0; i < products.length; i++){
        var p_opt = document.createElement("OPTION");
        p_opt.value = products[i].product_id;
        p_opt.text  = products[i].goods_attr_str;
        sp.options.add(p_opt);
      }
    }
    else{
      sp.style.display = 'none';

      var p_opt = document.createElement("OPTION");
      p_opt.value = 0;
      p_opt.text  = search_is_null;
      sp.options.add(p_opt);
    }
  }

  if (result.message.length > 0){
    alert(result.message);
  }
}

function change_good_products(){
	var filter = new Object;
	filter.goods_id = document.forms['theForm'].elements['goods_id'].value;

	//Ajax.call('snatch.php?is_ajax=1&act=search_products', filter, searchProductsResponse, 'GET', 'jsON');
	$.ajax({
		url: '__URL__/search_products',
		type: 'get',
		dataType: 'json',
		data: {'jsON':filter},
		beforeSend:function(){},
		success:function(msg){
			searchProductsResponse(msg);
		}		
	});
}

function searchProductsResponse(result){
  var frm = document.forms['theForm'];
  var sp = frm.elements['product_id'];

  if (result.error == 0){
    /* 清除 options */
    sp.length = 0;

    /* 创建 product options */
    var products = result.content.products;
    if (products.length){
      sp.style.display = display_yes;

      for (i = 0; i < products.length; i++){
        var p_opt = document.createElement("OPTION");
        p_opt.value = products[i].product_id;
        p_opt.text  = products[i].goods_attr_str;
        sp.options.add(p_opt);
      }
    }
    else{
      sp.style.display = 'none';

      var p_opt = document.createElement("OPTION");
      p_opt.value = 0;
      p_opt.text  = search_is_null;
      sp.options.add(p_opt);
    }
  }

  if (result.message.length > 0){
    alert(result.message);
  }
}

</script>